package com.yssoft.controller.publicPlatform;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yssoft.controller.BaseController;
import com.yssoft.service.production.ProductionProcessService;
/**
 * 
    * @ClassName: PublicTraceController
    * @Description: 公众溯源
    * @author lenovo
    * @date 2018年6月25日
    *
 */
import com.yssoft.service.publicPlatform.PublicTraceService;
import com.yssoft.service.purchase.RecipientDetailService;
import com.yssoft.service.supervise.ProductionDetectionService;
import com.yssoft.utils.AppUtil;

@Controller
@RequestMapping(value = "/public/trace")
public class PublicTraceController extends BaseController{
	private static Logger logger=Logger.getLogger(PublicTraceController.class);

	@Resource
	private RecipientDetailService recipientDetailService;
	@Resource
	private PublicTraceService publicTraceService;
	@Resource
    private ProductionDetectionService productionDetectionService;
    @Resource
    private ProductionProcessService productionProcessService;

	/**
	 * @方法名称: list
	 * @功能描述: 跳转到公众溯源(首次加载只查询产品信息)
	 * @作者: ljy
	 * @创建时间: 2018年6月10日 上午9:53:19
	 * @param model
	 * @return String
	 */
	@RequestMapping(value = "lableCodeTrace/{code}", method = RequestMethod.GET)
	public String trace(Model model,@PathVariable("code") String code){
		
		model=publicTraceService.lableCodeTrace(model,code);
		return "publicPlatform/publicTrace/lableCodeTrace";
	}
	/**
	 * @方法名称: productTrace
	 * @功能描述: 产品信息溯源
	 * @作者: ljy
	 * @创建时间: 2018年6月10日 上午9:53:19
	 * @return String
	 */
	@RequestMapping(value = "productTrace/{code}", method = RequestMethod.GET)
	public String productTrace(Model model,@PathVariable("code") String code){
		Map<String, Object>  map=publicTraceService.productTrace(code);
		model.addAttribute("product",map);
		model.addAttribute("code",code);
		return "publicPlatform/publicTrace/product";
	}
	/**
	 * @方法名称: productTrace
	 * @功能描述: 产品信息溯源
	 * @作者: ljy
	 * @创建时间: 2018年6月10日 上午9:53:19
	 * @return String
	 */
	@RequestMapping(value = "demandTrace/{code}", method = RequestMethod.GET)
	public String demandTrace(Model model,@PathVariable("code") String code){
		 //
		return "publicPlatform/publicTrace/demand";
	}
	/**
	 * @方法名称: productionTrace
	 * @功能描述: 生产信息溯源
	 * @作者: ljy
	 * @创建时间: 2018年6月10日 上午9:53:19
	 * @return String
	 */
	@RequestMapping(value = "productionTrace/{code}", method = RequestMethod.GET)
	public String productionTrace(Model model,@PathVariable("code") String code){
		Map<String, Object>  map=publicTraceService.productionTrace(code);
		model.addAttribute("demand",map);
		return "publicPlatform/publicTrace/production";
	}
	/**
	 * @方法名称: inspectionTrace
	 * @功能描述: 检验溯源
	 * @作者: ljy
	 * @创建时间: 2018年6月10日 上午9:53:19
	 * @return String
	 */
	@RequestMapping(value = "inspectionTrace/{code}", method = RequestMethod.GET)
	public String inspectionTrace(Model model,@PathVariable("code") String code){
		Map<String, Object>  map=publicTraceService.inspectionTrace(code);
		model.addAttribute("demand",map);
		return "publicPlatform/publicTrace/inspection";
	}
	/**
	 * @方法名称: circulationTrace
	 * @功能描述: 流通溯源
	 * @作者: ljy
	 * @创建时间: 2018年6月10日 上午9:53:19
	 * @return String
	 */
	@RequestMapping(value = "circulationTrace/{code}", method = RequestMethod.GET)
	public String circulationTrace(Model model,@PathVariable("code") String code){
		Map<String, Object>  map=publicTraceService.circulationTrace(code);
		model.addAttribute("demand",map);
		return "publicPlatform/publicTrace/lablesalebill";
	}
	/**
     * @创建人: ljy
     * @时间: 2018/6/27 0011 上午 10:26
     * @说明: 查询加工单工艺流程
     * @param workId
     * @param model
     * @return
     */
    @RequestMapping("technological")
    public String technological(String workId,Model model){
        List<Map<String,Object>>  productionProcessList =productionDetectionService.selectProductionProcessList(workId);
        for (Map<String, Object> stringObjectMap : productionProcessList) {
        	if(null==stringObjectMap.get("id") ||  "".equals(  stringObjectMap.get("id"))) {continue;}
            List<Map<String, Object>> index = productionProcessService.findIndexAndValueByProcessingTechnic( stringObjectMap.get("id")+ "",null);
            stringObjectMap.put("technicIndexList", index);
        }
        model.addAttribute("productionProcessList",productionProcessList);
        return "publicPlatform/publicTrace/technologicalProcess";
    }
    /**
     * @创建人: ljy
     * @时间: 2018/6/27 0011 上午 10:26
     * @说明: 收获溯源信息
     * @param workId
     * @param model
     * @return
     */
    @RequestMapping("recipientTrace")
    public String recipientTrace(String recipientId,Model model){
      List<Map<String, Object>> technologyList = publicTraceService.findTechnologyByRecipientId(recipientId);
      for (Map<String, Object> stringObjectMap : technologyList) {
          List<Map<String, Object>> index = recipientDetailService.findIndexAndValueByProcessingTechnic(stringObjectMap.get("id") + "",recipientId);
          stringObjectMap.put("technicIndex", index);
      }
      model.addAttribute("technologyList", technologyList) ;    
      return "publicPlatform/publicTrace/recipientTraceTechnological";
    }
	/**
	 * 获取溯源过程信息(公众端)
	 * @author ljy
	 * @param productId
	 * @return
	 */
/*	@ResponseBody
	@RequestMapping(value = "getTraceProcessInfo", method = RequestMethod.POST)
    public Object getTechnologyInfo(String recipientId,HttpServletRequest request) {
        try {
            List<Map<String, Object>> technologyList = publicTraceService.findTechnologyByProduct(request);
            for (Map<String, Object> stringObjectMap : technologyList) {
                List<Map<String, Object>> index = publicTraceService.findIndexAndValueByProcessingTechnic(stringObjectMap.get("id") + "",recipientId);
                stringObjectMap.put("technicIndex", index);
            }
            return AppUtil.returnListJson("1", "查询成功", technologyList);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AppUtil.returnMapJson("0", "查询失败", null);
        }
    }*/
}
